/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utain.xmlfilewriter2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Nut
 */
public class UtainXMLFileWriter2 {

    //StACK
    public static void main(String[] args) {
        String word[] = {
            "Time is more value than money, You can get more money, but you cannot get more time.",
            "เมื่อทำอะไรสำเร็จ แม้เป็นก้าวเล็กๆ ของตัวเอง ก็ควรรู้จักให้รางวัลตัวเองบ้าง",
            "You will never find time for anything. if you want the time, you must make it",
            "คนส่วนใหญ่เรียกร้องสิทธิมนุษยชน แต่คนมีปัญญาเรียกร้องสิทธิ์ที่จะไม่ทุกข์"};
        String by[] = {"Jim Rohn", "ว. วชิรเมธี", "Charles Buxton", "ว. วชิรเมธี"};
        try {
            createRssDoc(word, by);
            System.out.println("Saved......");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createRssDoc(String[] word, String[] by) throws Exception {
        String filename = "qoutes.xml";
        File file = new File(filename);
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = xof.createXMLStreamWriter(new OutputStreamWriter(new FileOutputStream(file)));
        xtw.writeStartDocument("UTF-8", "1.0");
        {
            xtw.writeStartElement("qoutes");
            {
                for (int i = 0; i < by.length; i++) {
                    xtw.writeStartElement("qoute");
                    {
                        xtw.writeStartElement("words");
                        xtw.writeCharacters(word[i]);
                        xtw.writeEndElement();

                        xtw.writeStartElement("by");
                        xtw.writeCharacters(by[i]);
                        xtw.writeEndElement();
                    }
                    xtw.writeEndElement();//end qoute}
                }
            }
            xtw.writeEndElement();//end qoutes
        }
        xtw.writeEndDocument();//end doc
        xtw.flush();
        xtw.close();
    }

}
